package com.sad.jetpack.v1.thememanager.demo;

import androidx.appcompat.app.AppCompatActivity;


import android.content.res.TypedArray;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sad.jetpack.v1.thememanager.api.BasicThemeDes;
import com.sad.jetpack.v1.thememanager.api.BasicThemeDesHelper;
import com.sad.jetpack.v1.thememanager.api.LocalTheme;
import com.sad.jetpack.v1.thememanager.api.ThemeManager;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    TextView tv2;
    RadioGroup rg_switcher;
    Button btn_save_curr_theme;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setupThemeListRadioGroup();
        testDynamic();
    }

    private void setupThemeListRadioGroup(){

        BasicThemeDesHelper.travelAllThemeDes(new BasicThemeDesHelper.OnTravelAllThemeDesCallback() {
            @Override
            public boolean onTraveling(BasicThemeDes basicThemeDes) {
                RadioButton button=new RadioButton(getApplicationContext());
                RadioGroup.LayoutParams rpl=new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                rpl.weight=1;
                button.setLayoutParams(rpl);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switchTheme(basicThemeDes);
                    }
                });
                button.setText(basicThemeDes.getName());
                rg_switcher.addView(button);
                if (LocalTheme.getLastLocalTheme(getApplicationContext()).getBasicThemeDes().getCode()== basicThemeDes.getCode()){
                    button.setChecked(true);
                }
                return false;
            }
        });
        /*btn_save_curr_theme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rg_switcher.getCheckedRadioButtonId();
            }
        });*/
    }

    private void switchTheme(BasicThemeDes des){
        /*ThemeManager.setActivityThemeByDesCode(this,des.getCode());
        LocalTheme.saveCurrLocalTheme(getApplicationContext(),new LocalTheme());
        BasicThemeDes.saveLastInsideThemeDes(getApplicationContext(),des);
        recreate();*/
    }

    private void initView(){
        tv=findViewById(R.id.tv);
        tv2=findViewById(R.id.tv2);
        rg_switcher=findViewById(R.id.switch_curr_theme);
        btn_save_curr_theme=findViewById(R.id.save_curr_theme);
    }

    private void testDynamic(){

//        ThemeManager.dynamicConfigCurrentInsideTheme(getApplicationContext())
//                //.styleAble(com.sad.jetpack.v1.thememanager.api.R.styleable.s_theme_text_attrs)
//                .addAttr(com.sad.jetpack.v1.thememanager.api.R.attr.color_dark, com.sad.jetpack.v1.thememanager.api.R.attr.card_background_color_level_1)
//                .perform(new IThemeAction() {
//                    @Override
//                    public void onApplyInsideResourceTheme(int styleId, TypedArray typedArray) {
//                        boolean isDark=typedArray.getBoolean(0,false);
//                        Log.e("eeee","----------->深色模式:"+isDark);
//                        int navColor=typedArray.getColor(0x00000001,0x00000000);
//                        Log.e("eeee","----------->导航栏用色:"+navColor);
//                        //eeeetv2.setTextColor(typedArray.getColor(com.sad.jetpack.v1.thememanager.api.R.styleable.s_theme_text_attrs_text_color_level_2,0xff000000));
//                    }
//                });


        TypedArray typedArray=ThemeManager.dynamicConfigCurrentInsideTheme(this)
                .addAttr(com.sad.jetpack.v1.thememanager.api.R.attr.color_dark,com.sad.jetpack.v1.thememanager.api.R.attr.card_background_color_level_1)
                .getTypeArray();
        //TypedArray typedArray=obtainStyledAttributes(new int[]{com.sad.jetpack.v1.thememanager.api.R.attr.color_dark,com.sad.jetpack.v1.thememanager.api.R.attr.card_background_color_level_1,com.sad.jetpack.v1.thememanager.api.R.attr.card_background_color_level_2});
        boolean isDark=typedArray.getBoolean(0,false);
        Log.e("eeee","----------->深色模式:"+isDark);
        int navColor=typedArray.getColor(1,0x00000000);
        Log.e("eeee","----------->导航栏用色:"+navColor);
        typedArray.recycle();
    }

}